import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

//TODO felul vizsgalni
public class OnGameHandParser extends HandParser {

	public OnGameHandParser(String uri, String name) {
		super(uri,name);
	}

	public GameInfo ReadFile(String s)
	{
		GameInfo gi=new GameInfo();
		String file=null;
		if(s.contains("S-2711") 
				|| s.contains("S-2184") 
				|| s.contains("S-2181") 
				|| s.contains("S-2440") 
				|| s.contains("S-2658")
				|| s.contains("S-2170")
				|| s.contains("R-")
				|| s.contains("S-")==false
				)
			return null;
		try {
			file=FileHandling.FiletoString(s);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		String temp="";
		
		//date
		gi.date=Calendar.getInstance();
		SimpleDateFormat sdf=new SimpleDateFormat("dd hh:mm:ss Z y");
		
		try {
			temp = file.substring(
					file.indexOf("Start hand: ") + "Start hand: ".length(),
					file.indexOf("\n", file.indexOf("Start hand:")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String month=temp.substring(temp.indexOf(' ')+1,temp.indexOf(' ')+4);
		temp=temp.replaceAll("GMT", "");
		temp=temp.substring(temp.indexOf(' ', temp.indexOf(' ')+2));
		temp=temp.replaceFirst(" ", "");
		try {
			gi.date.setTime(sdf.parse(temp));
			gi.date.add(Calendar.MONTH, MonthtoInt(month));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		//edate
		gi.edate=Calendar.getInstance();
		temp=file.substring(file.lastIndexOf("Start hand: ")+"Start hand: ".length(), file.indexOf("\n", file.lastIndexOf("Start hand:")));
		month=temp.substring(temp.indexOf(' ')+1,temp.indexOf(' ')+4);
		temp=temp.replaceAll("GMT", "");
		temp=temp.substring(temp.indexOf(' ', temp.indexOf(' ')+2));
		temp=temp.replaceFirst(" ", "");
		try {
			gi.edate.setTime(sdf.parse(temp));
			gi.edate.add(Calendar.MONTH, MonthtoInt(month));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		//duration
		gi.duration=Calendar.getInstance();
		gi.duration.setTimeInMillis(gi.edate.getTimeInMillis()-gi.date.getTimeInMillis());
		
		//players
		temp=file.substring(file.indexOf("Players in round: ")+"Players in round: ".length(), file.indexOf("\r\n", file.indexOf("Players in round: ")));
		gi.players=Short.parseShort(temp);
		
		//finnishing
		short szam;
		temp=file.substring(file.lastIndexOf("Start hand:"));
		String seged=temp.substring(temp.indexOf("Players in round: ")+"Players in round: ".length(),
				temp.indexOf("\r\n",temp.indexOf("Players in round: ")));
		String nick=temp.substring(temp.indexOf("Dealing to ")+"Dealing to ".length(),temp.indexOf(":",temp.indexOf("Dealing to ")));
		szam=Short.parseShort(seged);
		temp=temp.substring(temp.lastIndexOf(nick+" ("),temp.indexOf(",",temp.lastIndexOf(nick+" (")));
		temp=temp.substring(temp.indexOf('(')+1, temp.indexOf(')'));
		if(temp.equals("0"))
			gi.finishing=szam;
		else
			gi.finishing=1;

		//gamenum
//		seged=file.substring(file.indexOf("***** History for hand ")+"***** History for hand ".length());
//		temp=seged.substring(0,seged.indexOf(" "));
//		temp=temp.substring(temp.indexOf('-')+1,temp.lastIndexOf('-'));
		gi.gamenum=Long.parseLong(s.substring(s.indexOf('-')+1,s.lastIndexOf('-'))
				+s.substring(s.lastIndexOf('-')+1,s.lastIndexOf('.')));
		
		//roomname
		gi.roomname=this.getName();
		
		return gi;
		
		
	}
	
	@Override
	public ArrayList<GameInfo> Reader() {
		ArrayList<GameInfo> lista=new ArrayList<GameInfo>();
		File maindir=new File(Uri+"/tournaments");
		
		try {
			for(String s: maindir.list())
			{
				lista.add(this.ReadFile(maindir.getPath()+"/"+s));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		int i=0;
		while(i<lista.size())
		{
			if(lista.get(i)==null)
				lista.remove(i);
			else
				i++;
		}
		
		return lista;
	}

}
